#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/29 22:25
# @Author  : Nabby
# @File    : user_impl.py.py
# @Desc    :
from itertools import count

from app import db
import uuid
from infrastructure.po.mode.dict_data import DictData

class DictDataImpl(object):
    @classmethod
    def get_dict_data_list(cls, dict_type_id, page=1, page_size=999):
        query = db.session.query(DictData).filter(DictData.dict_type_id == dict_type_id)
        return query.order_by(DictData.create_time.desc()).paginate(page=page, per_page=page_size).items, query.count()

    @classmethod
    def add_dict_data(cls, dict_label, dict_value, dict_sort, dict_type_id, remark=None):
        count = db.session.query(DictData).filter(
            DictData.dict_label == dict_label,
            DictData.dict_type_id == dict_type_id,
        ).count()
        if count == 0:
            dict_data = DictData(
                id=str(uuid.uuid4()),
                dict_label=dict_label,
                dict_value=dict_value,
                dict_sort=dict_sort,
                dict_type_id=dict_type_id,
                status=1,
                remark=remark
            )
            db.session.add(dict_data)
            db.session.flush()
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def update_dict_data(cls, dict_data_id, dict_label, dict_value, dict_sort, dict_type_id, remark=None):
        count = db.session.query(DictData).filter(
            DictData.id!= dict_data_id,
            DictData.dict_label == dict_label,
            DictData.dict_type_id == dict_type_id).count()
        if count == 0:
            dict_data = db.session.query(DictData).filter(DictData.id == dict_data_id).first()
            dict_data.dict_label = dict_label
            dict_data.dict_value = dict_value
            dict_data.dict_sort = dict_sort
            dict_data.dict_type_id = dict_type_id
            dict_data.remark = remark
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def change_dict_status(cls, dict_data_id, status):
        dict_data = db.session.query(DictData).filter(DictData.id == dict_data_id).first()
        dict_data.status = status
        db.session.commit()


    @classmethod
    def delete_dict_data(cls, dict_data_id):
        dict_data = db.session.query(DictData).filter(DictData.id == dict_data_id).first()
        db.session.delete(dict_data)
        db.session.commit()




